//package org.ddd.pattern.event.consumer.interceptor.tt;
//
//import org.ddd.pattern.event.domain.DomainEvent;
//import org.ddd.pattern.event.springboot.rocketmq.consume.tt.IMessageContext;
//
//import java.lang.reflect.Method;
//import java.util.Arrays;
//import java.util.Map;
//import java.util.Optional;
//import java.util.concurrent.ConcurrentHashMap;
//
//public class HandlerMethodProvide {
//
//    private static final Map<String, Optional<Method>> subscribeMethods = new ConcurrentHashMap<>();
//    private final static String SUBSCRIBE_METHOD_NAME = "subscribe";
//
//
//    public static Optional<Method> cacheSubscribeMethodFor(Object handler,
//                                                           ISubscribeRequest subscribeRequest) {
//        final DomainEvent event = subscribeRequest.getMessageContext().getDomainEvent();
//        final Class<?> handlerType = handler.getClass();
//
//        Class<?> eventType = event.getClass();
//        String key = handlerType.getName() + ":" + eventType.getName();
//
//        if (subscribeMethods.containsKey(key)) {
//            return subscribeMethods.get(key);
//        }
//        synchronized (subscribeMethods) {
//            if (subscribeMethods.containsKey(key)) {
//                return subscribeMethods.get(key);
//            }
//            Optional<Method> subscribeMethod =
//                    Arrays.stream(handlerType.getDeclaredMethods())
//                            .filter(m -> m.getName().equals(SUBSCRIBE_METHOD_NAME))
//                            .filter(
//                                    m ->
//                                            m.getParameterCount() == 2
//                                                    && m.getParameterTypes()[0] == event.getClass()
//                                                    && m.getParameterTypes()[1].isAssignableFrom(IMessageContext.class))
//                            .findFirst();
//            if (!subscribeMethod.isPresent()) {
//                subscribeMethod =
//                        Arrays.stream(handlerType.getDeclaredMethods())
//                                .filter(m -> m.getName().equals(SUBSCRIBE_METHOD_NAME))
//                                .filter(
//                                        m ->
//                                                m.getParameterCount() == 2
//                                                        && m.getParameterTypes()[0].isAssignableFrom(event.getClass())
//                                                        && m.getParameterTypes()[1].isAssignableFrom(IMessageContext.class))
//                                .findFirst();
//            }
//
//            if (subscribeMethod.isPresent()) {
//                Method method = subscribeMethod.get();
//                method.setAccessible(true);
//                subscribeMethods.put(key, Optional.of(method));
//            } else {
//                subscribeMethods.put(key, Optional.empty());
//            }
//            return subscribeMethods.get(key);
//        }
//
//
//    }
//}
